package com.fz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fz.entity.RegionStores;
import com.fz.entity.UserRole;
import com.fz.mapper.RegionStoresMapper;
import com.fz.service.RegionStoresService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 区域门店关联表 服务实现类
 * </p>
 *
 * @author fz
 * @since 2024-08-26
 */
@Service
public class RegionStoresServiceImpl extends ServiceImpl<RegionStoresMapper, RegionStores> implements RegionStoresService {

    @Override
    public boolean save(Long regionId, Long[] storesIds) {
        UpdateWrapper<RegionStores> wrapper = new UpdateWrapper();
        wrapper.eq("region_id", regionId);
        this.remove(wrapper);
        List<RegionStores> list = new ArrayList<>();
        if (null != storesIds && storesIds.length != 0) {
            for (Long storesId : storesIds) {
                list.add(new RegionStores(regionId,storesId));
            }
        }
        return this.saveBatch(list);
    }

    @Override
    public List<RegionStores> getByRegionId(Long regionId) {
        QueryWrapper<RegionStores> wrapper = new QueryWrapper<>();
        wrapper.eq("region_id", regionId);
        return this.list(wrapper);
    }
}
